package com.yixincaipiao.demos;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.yixincaipiao.R;
import com.yixincaipiao.base.BaseActivity;
import com.yixincaipiao.weight.recyclerview.EndlessRecyclerOnScrollListener;
import com.yixincaipiao.weight.recyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.yixincaipiao.weight.recyclerview.LoadingFooter;
import com.yixincaipiao.weight.recyclerview.RecyclerViewStateUtils;
import com.yixincaipiao.weight.recyclerview.RecyclerViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */

public class ListActivity extends BaseActivity {

    private RecyclerView recycle;
    private SwipeRefreshLayout refresh_layout;
    ListDataAdapter adapter;
    private HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter = null;//实现添加头部问题的适配器


    public void initView() {
        setContentView(R.layout.activity_main);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        refresh_layout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        recycle.setLayoutManager(new LinearLayoutManager(this));

        adapter = new ListDataAdapter(this);
        mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(adapter);

        recycle.setAdapter(mHeaderAndFooterRecyclerViewAdapter);
        adapter.setData(buildData(), true);
        myRefreshData();//刷新
        recycle.addOnScrollListener(loadMoreListener);//加载更多监听
    }

    @Override
    protected void getData() {

    }

    //底部加载
    EndlessRecyclerOnScrollListener loadMoreListener = new EndlessRecyclerOnScrollListener() {
        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            if (RecyclerViewStateUtils.getFooterViewState(recycle) == LoadingFooter.State.Loading) {
                return;
            }

            //添加footer,判断是否是最后一页
            if (maxPage > currentPage) {//模拟数据

                RecyclerViewStateUtils.setFooterViewState(ListActivity.this, recycle, 15,//防止出现,单页时有loading问题
                        LoadingFooter.State.Loading);
                //加载数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("after", "加载更多");
                        adapter.setData(buildData(), false);
                        RecyclerViewStateUtils.setFooterViewState(recycle, LoadingFooter.State.Normal);
                    }
                }, 1000);
            } else {
                //最后一页时//yi  提示租后一页  二  不显示footer
                RecyclerViewUtils.removeFooterView(recycle);  //一出

            }

        }
    };

    public void myRefreshData() {
        refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("after", "shuaxin         refresh");
                        currentPage = 0;
                        adapter.setData(buildData(), true);
                        refresh_layout.setRefreshing(false);
                    }
                }, 700);
            }
        });
    }

    int currentPage = 0, maxPage = 49;

    public List<String> buildData() {
        List<String> dataList = new ArrayList<>();
        for (int i = currentPage; i < 25 + currentPage; i++) {
            dataList.add("数据    " + i);
        }
        currentPage += 25;
        return dataList;
    }
}
